EXIT Statement ---------------------------------------------------------------------------- Action Exits a DEF FN function, a DO... LOOP or FOR... NEXT loop, or a FUNCTION or SUB procedure. Syntax EXIT { DEF | DO | FOR | FUNCTION | SUB} Remarks There are a number of ways to use the EXIT statement, as described in the following list. ----------------------------------------------------------------------------- Statement Description ---------------------------------------------------------------------------- EXIT DEF Causes an immediate exit from the executing DEF FN function. Program execution continues where the DEF FN function was invoked. EXIT DO Provides an alternative exit from a DO... LOOP statement. Can be used only inside a DO... LOOP statement; EXIT DO transfers control to the statement following the LOOP statement. When used within nested DO... LOOP statements, transfers out of the immediately enclosing loop. EXIT FOR Provides another way to exit a FOR... NEXT loop. May appear only in a FOR... NEXT loop; transfers control to the statement following Statement Description ---------------------------------------------------------------------------- control to the statement following the NEXT statement. When used within nested FOR... NEXT loops, transfers out of the immediately enclosing loop. EXIT FUNCTION Causes an immediate exit from a FUNCTION procedure. Program execution continues where the procedure was invoked. Can be used only in a FUNCTION procedure. EXIT SUB Immediately exits a SUB procedure. Program execution continues with the statement after the CALL statement. Can be used only in a SUB procedure. None of the EXIT statements defines the end of the structure in which it is used. EXIT statements provide only an alternative exit from the structure. See Also DEF FN, DO... LOOP, FOR... NEXT, FUNCTION, SUB Example The following example demonstrates the use of a variety of EXIT statements. A loop is continuously executed until a key is pressed. Once a key is pressed, the next EXIT statement that executes will cause the program to end. DECLARE SUB ExitDemo () CLS DO PRINT . PRINT "Entering-Re-entering ExitDemo" ExitDemo SLEEP 1 LOOP WHILE INKEY$ = "" PRINT "Exiting EXIT statement programming example." END SUB ExitDemo DO FOR I% = 1 TO 1000 Num% = INT(RND * 100) SELECT CASE Num% CASE 7 PRINT "Exiting FOR...NEXT loop in ExitDemo SUB" EXIT FOR CASE 29 PRINT "Exiting DO...LOOP in ExitDemo SUB" EXIT DO CASE 54 PRINT "Exiting ExitDemo SUB" EXIT SUB CASE ELSE END SELECT NEXT I% LOOP END SUB